package com.jereh.topnews.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Administrator on 2016/11/25.
 */
public class DBHelper extends SQLiteOpenHelper {

    private static DBHelper dbHelper;
    private static SQLiteDatabase db;

    public DBHelper(Context context) {
        super(context, "topnews.db", null, 1);
    }

    public static DBHelper getDbHelper(Context context){
        if(dbHelper==null) {
            dbHelper=new DBHelper(context);
            db=dbHelper.getWritableDatabase();
        }
        return dbHelper;
    }

    public static SQLiteDatabase getDb() {
        return db;
    }

    /**
     * 创建数据库操作
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        //获取db对象后进行数据库初始化工作
        db.execSQL("create table if not exists channel(id integer primary key autoincrement,channelId varchar(30),name varchar(30))");
        db.execSQL("create table if not exists content(id integer primary key autoincrement,link varchar(100)," +
                "title varchar(100)," +
                "pubDate varchar(50),channelName varchar(50)," +
                "desc varchar(200),source varchar(100),reply int," +
                "html varchar(2000) )");
        db.execSQL("create table if not exists imageurl (id integer primary key autoincrement,link varchar(100)," +
                "url varchar(50),height int,width int)");
    }

    /**
     * 更新数据库操作
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists channel");
        db.execSQL("drop table if exists content");
        db.execSQL("drop table if exists imageurl");
        onCreate(db);
    }

}
